package com.dingshi.security.db;


import org.json.JSONException;
import org.json.JSONObject;

import com.dingshi.security.config.HttpClientConfig;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class SettingLoader {
	
	private static final String CONFIG = "settings";
	private static final String USERINFO = "user_info";
	private static final String REGISTER = "register";
	private static final String SVR_PATH = "svrpath";
	private static final String UPDATE_PATH = "updatepath";
	private static final String TIMER = "timer";

	/**
	 * 用户名
	 * @param context
	 * @param email
	 * @return
	 */
	public static boolean setUserInfo(Context context,String userinfo) {
		SharedPreferences preferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
		Editor edit = preferences.edit();
		edit.putString(USERINFO, userinfo);
		return edit.commit();
	}
	
	public static String getUserInfo(Context context) {
		SharedPreferences preferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
		return preferences.getString(USERINFO, null);
	}
	
	public static boolean hasregister(Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
		return sharedPreferences.getBoolean(REGISTER, true);
	}

	public static void setRegister(Context context, boolean register) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
		Editor edit = sharedPreferences.edit();
		edit.putBoolean(REGISTER, register);
		edit.commit();
	}
	
	/**
	 * 获取部门编号
	 * @param context
	 * @return
	 */
	public static String getDepartNum(Context context) {
		if(getUserInfo(context)==null||"".equals(getUserInfo(context))) {
			return null;
		}
		try {
			JSONObject json = new JSONObject(getUserInfo(context));
			return json.getString("did");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取保安编号
	 * @param context
	 * @return
	 */
	public static String getGuardNum(Context context) {
		if(getUserInfo(context)==null||"".equals(getUserInfo(context))) {
			return null;
		}
		try {
			JSONObject json = new JSONObject(getUserInfo(context));
			return json.getString("nid");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取保安名字
	 * @param context
	 * @return
	 */
	public static String getGuardName(Context context) {
		if(getUserInfo(context)==null||"".equals(getUserInfo(context))) {
			return null;
		}
		try {
			JSONObject json = new JSONObject(getUserInfo(context));
			return json.getString("n");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 获取地区小区编号
	 * @param context
	 * @return
	 */
	public static String getAreaNum(Context context) {
		if(getUserInfo(context)==null||"".equals(getUserInfo(context))) {
			return null;
		}
		try {
			JSONObject json = new JSONObject(getUserInfo(context));
			return json.getString("did");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 获取地区小区名称
	 * @param context
	 * @return
	 */
	public static String getAreaName(Context context) {
		if(getUserInfo(context)==null||"".equals(getUserInfo(context))) {
			return null;
		}
		try {
			JSONObject json = new JSONObject(getUserInfo(context));
			return json.getString("a");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取登录帐号类型
	 * @param context
	 * @return
	 */
	public static String getLoginUserType(Context context) {
		if(getUserInfo(context)==null||"".equals(getUserInfo(context))) {
			return null;
		}
		try {
			JSONObject json = new JSONObject(getUserInfo(context));
			return json.getString("t");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return "";
	}
	/**
	 * 获取保安值班类型 0白班 1夜班
	 * @param context
	 * @return
	 */
	public static String getGuardType(Context context) {
		if(getUserInfo(context)==null||"".equals(getUserInfo(context))) {
			return null;
		}
		try {
			JSONObject json = new JSONObject(getUserInfo(context));
			return json.getString("ty");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static boolean setSvrPath(Context context,
			String svrpath, String updatepath) {
		SharedPreferences preferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
		Editor edit = preferences.edit();
		edit.putString(SVR_PATH, svrpath);
		edit.putString(UPDATE_PATH, updatepath);
		return edit.commit();
	}
	
	public static String getSvrPath(Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
		return sharedPreferences.getString(SVR_PATH, HttpClientConfig.DOMAIN_NAME);
	}
	
	public static String getUpdateSvrPath(Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
		return sharedPreferences.getString(UPDATE_PATH, HttpClientConfig.DOMAIN_NAME);
	}

	public static boolean setTimer(Context context, boolean b) {
		SharedPreferences preferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
		Editor edit = preferences.edit();
		edit.putBoolean(TIMER, b);
		return edit.commit();
	}
	
	public static boolean getTimer(Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(CONFIG, Context.MODE_PRIVATE);
		return sharedPreferences.getBoolean(TIMER, false);
	}
	
}
